<template>
  <div :style="{ width, height }"></div>
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons");
import resize from './mixins/resize'
export default {
  mixins:[resize],
  name: "",
  data() {
    return {
      chart: null,
    };
  },
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    chartData: {
      type: Object,
      default: {},
    },
    yTittle: {
      type: String,
      required: true,
    },
  },

  mounted() {
    // 拿的是根元素
    this.chart = echarts.init(this.$el, "macarons");
    this.setOption(this.chartData);
  },
  methods: {
    setOption({expectedData = [],actualData = []}) {
      this.chart.setOption({
        // title: {
        //   text: "ECharts 入门示例",
        // },
        tooltip: {
          trigger:'axis',
          axisPointer:{
            type:'cross'
          }
        }, //提示
        //图例
        legend: {
          data: ["期望", "实际"],
        },
        // x轴
        xAxis: {
          data: ["周一", "周二", "周三", "周四", "周五", "周六","周日"],
          boundaryGap:false
        },
        //y轴
        yAxis: {
          name:this.yTittle
        },

        grid:{
          left:10,
          right:10,
          top:40,
          bottom:10,
          containLabel:true
        },

        //系列
        series: [
          {
            name: "期望",
            type: "line",
            data: expectedData,
            
            itemStyle:{
              color:"red"
            },
            lineStyle:{
              width:4
            },
            areaStyle:{
              color:'hotpink'
            }
          },
          {
            name: "实际",
            type: "line",
            data: actualData,
            itemStyle:{
              color:"blue"
            },
            lineStyle:{
              width:4
            }
          },
        ],
      });
    },
  },

  watch: {
    chartData(newVal, oldVal) {
      this.setOption(newVal);
    },
  },
};
</script>

<style lang="less" scoped>
</style>
